import json

from fastapi import APIRouter
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.allsubdb.npcflklaw.models import headers, DownClassModel,DownArticleModel
from apps.core.global_model import IdMarksModel, MarksModel
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo

router = APIRouter(route_class=ContextIncludedRoute)

@router.post("/step1/down_cnt")
async def down_cnt(input: InputInfoModel[DownClassModel]):
    """
    根据类别下载该类别记录总数
    :return:
    """
    return_info = ReturnInfo()
    id_ = input.data.id
    marks = input.data.marks
    proxy = input.proxy
    url = "https://flk.npc.gov.cn/api/?type={}&searchType=title%3Baccurate&sortTr=f_bbrq_s%3Bdesc&gbrqStart=&gbrqEnd=&sxrqStart=&sxrqEnd=&sort=true&page=1&size=10&_=1615425100810".format(id_)
    rrq = AioHttpRequest()
    this_header = headers.copy()
    rrq.set_url(url).set_proxy(proxy) \
        .set_timeout(30).set_marks(marks) \
        .set_header(this_header) \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    rsjson = json.loads(rrq.html)
    return_info.data = {"totalSizes": rsjson["result"]["totalSizes"]}
    return return_info.todict()

@router.post("/step2/down_class_list")
async def down_class_list(input: InputInfoModel[DownClassModel]):
    """
    下载某个分类 获取文章的列表,有分页 <br>
    :return:
    """
    return_info = ReturnInfo()
    id_ = input.data.id
    page = input.data.page
    marks = input.data.marks
    proxy = input.proxy
    url = "https://flk.npc.gov.cn/api/?type={}&searchType=title%3Baccurate&sortTr=f_bbrq_s%3Bdesc&gbrqStart=&gbrqEnd=&sxrqStart=&sxrqEnd=&sort=true&page={}&size=10&_=1615425100810".format(id_,page)
    rrq = AioHttpRequest()
    this_header = headers.copy()
    rrq.set_url(url).set_proxy(proxy) \
        .set_timeout(30).set_marks(marks) \
        .set_header(this_header) \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()

@router.post("/step3/down_article")
async def down_article(input: InputInfoModel[DownArticleModel]):
    """
    下载文章  <br>
    makrs: firstHeading-wrap <br>
    :return:
    """
    return_info = ReturnInfo()
    id_ = input.data.id
    url= r"https://flk.npc.gov.cn/api/detail"
    data = {
        "id" : id_
    }
    print(data)
    proxy = input.proxy
    marks = input.data.marks
    rrq = AioHttpRequest()
    this_header = headers.copy()
    rrq.set_url(url).set_proxy(proxy) \
        .set_timeout(30).set_marks(marks) \
        .set_header(this_header) \
        .set_data(data) \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.POST)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()
